package org.csu.tank.protocol;


import javafx.application.Platform;
import org.csu.tank.client.TankClient;
import org.csu.tank.components.Dir;
import org.csu.tank.components.PlayerComponent;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;

/**
 * 旧坦克向新坦克发送消息的协议
 */
public class TankAlreadyExistMsg implements Msg {
      private int msgType = Msg.TANK_ALREADY_EXIST_MSG;
      private TankClient tc;

      private PlayerComponent tankPlayer;

      public TankAlreadyExistMsg(PlayerComponent playerComponent) {
            this.tankPlayer = playerComponent;
      }

      public TankAlreadyExistMsg(TankClient tc) {
            this.tc = tc;
            this.tankPlayer = tc.getPlayerComponent();
      }

      @Override
      public void send(DatagramSocket ds, String IP, int UDP_Port) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream(88);
            DataOutputStream dos = new DataOutputStream(baos);
            try {
                  dos.writeInt(msgType);
                  dos.writeInt(tankPlayer.getId());
                  dos.writeDouble(tankPlayer.getX());
                  dos.writeDouble(tankPlayer.getY());
                  dos.writeInt(tankPlayer.getMoveDir().ordinal());
                  dos.writeBoolean(tankPlayer.isGood());
//            dos.writeUTF(tankPlayer.getName());
            } catch (IOException e) {
                  e.printStackTrace();
            }
            byte[] buf = baos.toByteArray();
            try {
                  DatagramPacket dp = new DatagramPacket(buf, buf.length, new InetSocketAddress(IP, UDP_Port));
                  ds.send(dp);
            } catch (IOException e) {
                  e.printStackTrace();
            }
      }

      @Override
      public void parse(DataInputStream dis) {

            try {
                  int id = dis.readInt();
                  if (id == tankPlayer.getId()) {
                        return;
                  }
                  boolean exist = false;

                  //TODO 遍历坦克队列检测是否已经存在
                  for (PlayerComponent tankPlayer : tc.getPlayerComponentList()) {
                        if (id == tankPlayer.getId()) {
                              exist = true;
                              break;
                        }
                  }

                  if (!exist) {
                        double x = dis.readDouble();
                        double y = dis.readDouble();
                        Dir dir = Dir.values()[dis.readInt()];
                        boolean good = dis.readBoolean();
//                        String name = dis.readUTF();

                        Platform.runLater(new Runnable() {
                              @Override
                              public void run() {
                                    tc.creatNewPlayer(id, x, y, dir, good);
                              }
                        });
                  }
            } catch (IOException e) {
                  e.printStackTrace();
            }
      }
}
